//
//  XQSmallCardView.swift
//  Spark Peso
//
//  Created by 小琦 on 2025/2/3.
//

import UIKit

class XQSmallCardView: UIView {
    
    var applyHandle: (() -> Void)?
    
    var topCardInfo : XQHomeReadingCaucasianModel? {
        didSet {
            self.titleLb.text = topCardInfo?.ranks
            self.dollorLb.text = topCardInfo?.pencil
            self.dayLb.text = "\(topCardInfo?.satirized ?? "") : \(topCardInfo?.guidelines ?? "")"
            self.rateLb.text = "\(topCardInfo?.lee ?? "") : \(topCardInfo?.daze ?? "")"
            self.gradientView.childView.setTitle(topCardInfo?.tested, for: .normal)
            
            if let url = URL(string: topCardInfo?.coarseness ?? "") {
                self.imageView.kf.setImage(with: url,placeholder: UIImage(named: "AppIcon"))
            }else {
                self.imageView.image = UIImage(named: "AppIcon")
            }
        }
    }

    private lazy var cardImageView : UIImageView = {
        let imageView = UIImageView()
        imageView.image = UIImage(named: "home_head_img")
        imageView.isUserInteractionEnabled = true
        imageView.addGestureRecognizer(UITapGestureRecognizer(target: self, action: #selector(applyClick)))
        return imageView
    }()
    
    private lazy var titleLb : UILabel = {
        let lb = UILabel()
        lb.font = UIFont.systemFont(ofSize: 16, weight: .bold)
        lb.textColor = .xq_black
        return lb
    }()
    
    private lazy var dollorLb : UILabel = {
        let lb = UILabel()
        lb.textAlignment = .center
        lb.textColor = .mainTextColor
        lb.font = UIFont.boldSystemFont(ofSize: 36)
        lb.adjustsFontSizeToFitWidth = true
        return lb
    }()
    
    private lazy var imageView : UIImageView = {
        let imageView = UIImageView()
        imageView.layer.cornerRadius = 7
        imageView.layer.masksToBounds = true
        return imageView
    }()
    
    private lazy var dayLb : UILabel = {
        let lb = UILabel()
        lb.textColor = .xq_black
        lb.font = UIFont.systemFont(ofSize: 11)
        return lb
    }()
    
    private lazy var rateLb : UILabel = {
        let lb = UILabel()
        lb.textColor = .xq_black
        lb.font = UIFont.systemFont(ofSize: 11)
        return lb
    }()
    
    private lazy var gradientView : XQCornerShadowView<XQGradientButton> = {
        let cornerShadowView = XQCornerShadowView<XQGradientButton>(frame: CGRect(x: 40, y: 0, width: UIDevice.xq_width() - 80, height: 50))
        cornerShadowView.childView.setTitle("Apply Now", for: .normal)
        cornerShadowView.childView.titleLabel?.font = UIFont.boldSystemFont(ofSize: 20)
        cornerShadowView.childView.colors = [UIColor(hex:"#B465FC")!.cgColor,UIColor(hex:"#9521F1")!.cgColor]
        cornerShadowView.childView.cornerRadius = 28
        cornerShadowView.layer.shadowColor = UIColor.white.cgColor
        cornerShadowView.layer.shadowOffset = CGSize(width: 0, height: 1)
        cornerShadowView.layer.shadowRadius = 6
        cornerShadowView.layer.shadowOpacity = 0.8
        return cornerShadowView
    }()
    
    private lazy var btn : UIButton = {
        let btn = UIButton(type: .custom)
        btn.addTarget(self, action: #selector(applyClick), for: .touchUpInside)
        return btn
    }()
    
    override init(frame: CGRect) {
        super.init(frame: frame)
        self.backgroundColor = .clear
        makeUI()
        makeConstraints()
    }
    
    required init?(coder: NSCoder) {
        super.init(coder: coder)
        makeUI()
        makeConstraints()
    }
    
    func makeUI() {
        self.addSubview(self.cardImageView)
        self.cardImageView.addSubview(self.titleLb)
        self.cardImageView.addSubview(self.imageView)
        self.cardImageView.addSubview(self.dollorLb)
        self.cardImageView.addSubview(self.dayLb)
        self.cardImageView.addSubview(self.rateLb)
        self.addSubview(self.gradientView)
        self.gradientView.addSubview(self.btn)
    }
    
    func makeConstraints() {
        self.cardImageView.snp.makeConstraints { make in
            make.top.equalTo(0)
            make.left.equalTo(0)
            make.right.equalTo(0)
            make.bottom.equalTo(-25)
        }
        self.titleLb.snp.makeConstraints { make in
            make.top.equalTo(85)
            make.left.equalTo(36)
        }
        self.imageView.snp.makeConstraints { make in
            make.right.equalTo(-40)
            make.top.equalTo(86)
            make.width.height.equalTo(36)
        }
        self.dollorLb.snp.makeConstraints { make in
            make.left.equalTo(self.titleLb.snp.left)
            make.top.equalTo(self.titleLb.snp.bottom)
            make.right.lessThanOrEqualTo(self.imageView.snp.left).offset(-12)
        }
        self.dayLb.snp.makeConstraints { make in
            make.top.equalTo(self.dollorLb.snp.bottom).offset(7)
            make.left.equalTo(36)
        }
        self.rateLb.snp.makeConstraints { make in
            make.right.equalTo(-40)
            make.centerY.equalTo(self.dayLb)
        }
        self.gradientView.snp.makeConstraints { make in
            make.bottom.equalTo(0)
            make.height.equalTo(50)
            make.left.equalTo(24)
            make.right.equalTo(-24)
        }
        self.btn.snp.makeConstraints { make in
            make.edges.equalToSuperview()
        }
    }
    
    @objc private func applyClick() {
        applyHandle?()
    }
}
